package com.github.gdxmap.layer.vector;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.PolygonRegion;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.EarClippingTriangulator;
import com.github.gdxmap.geo.LatLng;
import com.github.gdxmap.geometry.Point;

import java.util.List;

/**
 * Created by free on 2016/12/7 0007.
 */

public class Polygon extends com.badlogic.gdx.math.Polygon implements VectorNode {
    //是否填充
    private boolean fill=true;
    //填充颜色
    private Color fillColor=Color.SKY;
    //边框颜色
    private Color borderColor=Color.BLUE;
    //边框线宽度
    private int borderWeight=1;
    //中心点经纬度
    private LatLng latLng;

    private PolygonRegion polygonRegion;

    private Pixmap pix;

    public Polygon(){
        setVertices(new float[]{0,0,0,0,0,0});
    }

    public Polygon(float[] points){
        setVertices(points);
    }

    public Polygon(List<Point> points){
        setPoints(points);
    }

    public void setPoints(List<Point> points){
        float[] localVertices=new float[points.size()*2];

        for(int i=0;i<localVertices.length;i=i+2){
            Point point=points.get(i/2);

            localVertices[i]=point.x;
            localVertices[i+1]=point.y;
        }

        setVertices(localVertices);
    }

    public boolean isFill() {
        return fill;
    }

    public void setFill(boolean fill) {
        this.fill = fill;
    }

    public Color getFillColor() {
        return fillColor;
    }

    public void setFillColor(Color fillColor) {
        this.fillColor = fillColor;
    }

    public Color getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(Color borderColor) {
        this.borderColor = borderColor;
    }

    public int getBorderWeight() {
        return borderWeight;
    }

    public void setBorderWeight(int borderWeight) {
        this.borderWeight = borderWeight;
    }

    public LatLng getLatLng() {
        return latLng;
    }

    public void setLatLng(LatLng latLng) {
        this.latLng = latLng;
    }

    @Override
    public void renderFill(PolygonSpriteBatch polygonSpriteBatch) {
        drawFillPolygon(polygonSpriteBatch,getVertices(),getPixMap(new Color(fillColor)));
    }
    private void drawFillPolygon(PolygonSpriteBatch polygonSpriteBatch,float[] vertices,Pixmap pix){
        if (polygonRegion==null) {
            polygonRegion = new PolygonRegion(new TextureRegion(new Texture(pix)),
                    vertices, new EarClippingTriangulator().computeTriangles(vertices).toArray());
        }
        polygonSpriteBatch.draw(polygonRegion,0,0);
    }
    private Pixmap getPixMap(Color color){
        if (pix==null) {
            pix = new Pixmap(1, 1, Pixmap.Format.RGB565);
            pix.setColor(color);
            pix.fill();
        }
        return pix;
    }
}
